package org.qiancizhan.Controller;

import com.google.gson.Gson;
import com.alibaba.dashscope.common.Role;
import org.qiancizhan.API.ALiYun_TongYiAi;
import org.qiancizhan.Config.RedisConfig;
import org.qiancizhan.Mapper.AIConversationMapper;
import org.qiancizhan.Model.DTO.AIConversationContentDTO;
import org.qiancizhan.Model.POJO.AIConversationContentPOJO;
import org.qiancizhan.Model.POJO.AIConversationPOJO;
import org.qiancizhan.Model.POJO.UserPOJO;
import org.qiancizhan.Utils.RandomIDGenerateUtil;
import org.qiancizhan.Utils.ThreadLocalUtil;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class AIConversationController {
    private final AIConversationMapper aiConversationMapper = new AIConversationMapper();
    private final Jedis redis = RedisConfig.getInstance();
    private final Gson gson = new Gson();
    private final ALiYun_TongYiAi aliYun_tongYiAi = new ALiYun_TongYiAi();

    public void XiaoAiInit() {
        UserPOJO user = ThreadLocalUtil.get();
        List<AIConversationContentDTO> userMessages = new ArrayList<>();
        userMessages.add(new AIConversationContentDTO(RandomIDGenerateUtil.generateRandomID(),Role.SYSTEM.getValue(), """
                你是一名英语专业老师，你的名字叫小爱
                你可以帮助学生进行：
                语法解析
                词汇指导
                发音纠正
                写作辅导
                口语练习
                文化背景
                我会用简单语言解释
                每个知识点都有例子
                鼓励你多提问
                适当给你鼓励
                音标用国际音标
                例句贴近生活
                解释完我会问：
                还有哪里不懂的疑问吗？
                我只用中文回答
                除非你要英文回复
                解答完后可以加个颜文字表情更加生动
                例如，还有什么需要解答的吗？[(●'◡'●)]
                回复要简短些，类似英语老师那样
                每行话不能超过20字，超过20字就换行
                        """));
        String userKey = "user_" + user.getId();
        redis.del(userKey);
        for (AIConversationContentDTO message : userMessages)
            redis.rpush(userKey, gson.toJson(message));
        redis.expire(userKey, 24 * 60 * 60);
    }

    public List<AIConversationContentDTO> sendMessage(String msg) {
        UserPOJO user = ThreadLocalUtil.get();
        String userKey = "user_" + user.getId();
        List<String> messageJsons = redis.lrange(userKey, 0, -1);
        List<AIConversationContentDTO> allMessages = new ArrayList<>();
        for (String messageJson : messageJsons) {
            AIConversationContentDTO message = gson.fromJson(messageJson, AIConversationContentDTO.class);
            allMessages.add(message);
        }
        AIConversationContentDTO userMessage = new AIConversationContentDTO(RandomIDGenerateUtil.generateRandomID(), "user", msg);
        allMessages.add(userMessage);
        List<AIConversationContentDTO> aiResponse = aliYun_tongYiAi.sendMessage(allMessages).join();
        redis.del(userKey);
        for (AIConversationContentDTO message : aiResponse)
            redis.rpush(userKey, gson.toJson(message));
        redis.expire(userKey, 24 * 60 * 60);
        return aiResponse;
    }

    public String generateTitle(List<AIConversationContentDTO> messages) {
        messages.add(new AIConversationContentDTO(RandomIDGenerateUtil.generateRandomID(), "system", "请生成一个对话标题,纯文本,字数在15字以内"));
        List<AIConversationContentDTO> aiResponse = aliYun_tongYiAi.sendMessage(messages).join();
        return aiResponse.get(aiResponse.size() - 1).getMessage_content();
    }

    public String addAIConversation(boolean isXiaoAi, String title, int message_count) {
        return aiConversationMapper.addAIConversation(isXiaoAi,title,message_count);
    }

    public List<AIConversationPOJO> getUserAIConversationList() {
        return aiConversationMapper.getUserAIConversationList();
    }

    public List<AIConversationContentPOJO> getUserAIConversationContents(String conversationId) {
        return aiConversationMapper.getUserAIConversationContents(conversationId);
    }

    public void addAIcontent(String conversationID, List<AIConversationContentDTO> messages) {
        aiConversationMapper.addAIcontent(conversationID, messages);
    }

    public void clearConversation() {
        aiConversationMapper.clearConversation();
    }
}
